package com.puppet.config.redis.run;

import com.common.puppet.BookConst;
import com.puppet.pojo.BookType;
import com.puppet.service.BookService;
import com.puppet.service.BookTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class InitTopSaleRunner implements CommandLineRunner {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookTypeService bookTypeService;

    // 热销榜数据初始化
    @Override
    public void run(String... args) throws Exception {
        bookTypeService.findAll();
        log.info("图书类别数据初始化完成");

        bookService.findTopSales(BookConst.FIRST_PAGE, BookConst.PAGE_SIZE);
        log.info("热销榜数据初始化完成");

        List<Integer> typeIds = bookTypeService.findByTableId();
        for (Integer typeId : typeIds) {
            bookService.findByType(BookConst.FIRST_PAGE, BookConst.PAGE_SIZE, typeId);
        }
        log.info("图书类别数据初始化完成");
    }
}
